﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms;
using System.Threading;
using System.Drawing;

namespace Tool
{
  public class WriteLog
     
    {

        #region Log日志
        private static object m_Lock = new object();

        /// <summary>
        /// Log日志 
        /// </summary>
        /// <param name="Path">路径</param>
        /// <param name="Text">消息</param>
        public void Log(string Path, string Text)
        {
            lock (m_Lock)
            {
                if (!Directory.Exists(Path))//判断文件夹是否存在
                {
                    //创建文件夹
                    Directory.CreateDirectory(Path);
                }
                //文件名 
                string logPath = Path + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";

                try
                {

                    using (StreamWriter sw = File.AppendText(logPath))
                    {
                        sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + Text);//写入内容
                        sw.Flush();//清理当前写入器的所有缓冲区
                        sw.Close();//关闭流
                        sw.Dispose();//释放资源
                    }
                }
                catch (IOException ex)
                {
                    using (StreamWriter sw = File.AppendText(logPath))
                    {
                        sw.WriteLine("时间：" + ex.Message + DateTime.Now.ToString("yyy-MM-dd HH:mm:ss"));
                        sw.Flush();
                        sw.Close();
                        sw.Dispose();
                    }
                }
            }

        }

        #endregion

        #region 删除文件夹
        /// <summary>
        /// 删除文件夹
        /// </summary>
        /// <param name="fileDirect"></param>
        /// <param name="saveDay"></param>
        private void DeleteFile(string fileDirect, int saveDay)
        {
            DateTime nowTime = DateTime.Now;
            DirectoryInfo root = new DirectoryInfo(fileDirect);
            DirectoryInfo[] dics = root.GetDirectories();//获取文件夹

            FileAttributes attr = File.GetAttributes(fileDirect);
            if (attr == FileAttributes.Directory)//判断是不是文件夹
            {
                foreach (DirectoryInfo file in dics)//遍历文件夹
                {
                    TimeSpan t = nowTime - file.CreationTime;  //当前时间  减去 文件创建时间
                    int day = t.Days;
                    if (day > saveDay)   //保存的时间 ；  单位：天
                    {

                        Directory.Delete(file.FullName, true);  //删除超过时间的文件夹
                    }
                }
            }
        }
        //可以将该类放在软件启动时或计时器里，达到定时清除log文件夹功能：
        private void timer3_Tick(object sender, EventArgs e)
        {
            DeleteFile(System.Environment.CurrentDirectory + @"\log\", 1); //删除该目录下 超过 1天的文件
        }

        #endregion

        #region 删除日志
        public static void DeleteLog(string sLogPath)
        {
            DirectoryInfo dyInfo = new DirectoryInfo(sLogPath);
            //获取文件夹下所有的文件
            foreach (FileInfo feInfo in dyInfo.GetFiles())
            {
                //判断文件日期是否小于指定日期，是则删除
                if (feInfo.CreationTime < DateTime.Now.AddDays(-7))
                    feInfo.Delete();
            }
        }

        #endregion

        public static void Dellogs()
        {

            Task.Factory.StartNew(() => {
                var path = AppDomain.CurrentDomain.BaseDirectory + "logs\\"; //文件夹路径
                if (!Directory.Exists(path)) return;

                var dyInfo = new DirectoryInfo(path);
                foreach (var feInfo in dyInfo.GetFiles("*.log"))
                {
                    if (feInfo.LastWriteTime < DateTime.Now.AddDays(-7)) feInfo.Delete();
                }
                Thread.Sleep(1000 * 60 * 60 * 24);//24小时执行一次
                Dellogs();//递归
            });
        }

    }

    #region 控件库
    public class CtrDisplay
    {
        public static string[] RtbStrings
        {
            get;
            private set;
        }
        /// <summary>
        /// RichTextBox最大追加内容的次数
        /// </summary>
        private const int RtbMaxAdd = 1000;
        /// <summary>
        /// RichTextBox当前追加的次数
        /// </summary>
        private static int RtbAddCount { set; get; }

        public static void button_MouseEnter(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            btn.Font = new Font("宋体", 9, FontStyle.Bold);
            btn.ForeColor = Color.Green;
            btn.FlatAppearance.BorderSize = 1;

        }

        public static void button_MouseLeave(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            btn.Font = new Font("宋体", 9);
            btn.ForeColor = SystemColors.ControlText;
            btn.FlatAppearance.BorderSize = 0;
        }

        /// <summary>
        /// 在Form窗体上修改Text属性值
        /// </summary>
        /// <param name="form">窗体名称</param>
        /// <param name="text">需要显示的文本</param>
        public static void ShowText(Form form, string text)
        {
            if (form.InvokeRequired)
            {
                form.Invoke(new Action<Form, string>((frm, str) => ShowText(frm, str)), form, text);
                return;
            }
            form.Text = text;

        }
        /// <summary>
        /// 在StatusStrip控件的子控件上修改Text属性值
        /// </summary>
        /// <param name="statusControl">StatusStrip的控件名称</param>
        /// <param name="index">子控件的序列号</param>
        /// <param name="text">需要显示的文本</param>
        public static void ShowText(StatusStrip statusControl, int index, string text)
        {
            if (statusControl.InvokeRequired)
            {
                statusControl.Invoke(new Action<StatusStrip, int, string>((sts, i, str) => ShowText(sts, index, str)), statusControl, index, text);
                return;
            }
            if (statusControl.Items.Count > index)
            {
                statusControl.Items[index].Text = text;
            }

        }
        /// <summary>
        /// 在StatusStrip控件的子控件上修改Text，背景色和前景色，如果前景色或者背景色值为Color.Empty时，则不发生改变
        /// </summary>
        /// <param name="statusControl">StatusStrip的控件名称</param>
        /// <param name="index">子控件的序列号</param>
        /// <param name="text">需要显示的文本</param>
        /// <param name="backColor">背景颜色</param>
        /// <param name="foreColor">前景色</param>
        public static void ShowText(StatusStrip statusControl, int index, string text, Color backColor, Color foreColor)
        {
            if (statusControl.InvokeRequired)
            {
                statusControl.Invoke(new Action<StatusStrip, int, string, Color, Color>((sts, i, str, bclr, fclr) => ShowText(sts, index, str, bclr, fclr)), statusControl, index, text, backColor, foreColor);
                return;
            }
            if (statusControl.Items.Count > index)
            {
                statusControl.Items[index].Text = text;
                if (backColor != Color.Empty)
                {
                    statusControl.Items[index].BackColor = backColor;
                }
                if (foreColor != Color.Empty)
                {
                    statusControl.Items[index].ForeColor = foreColor;
                }
            }
        }
        /// <summary>
        /// 在ToolStripStatusLabel上修改Text属性值
        /// </summary>
        /// <param name="stslabel">ToolStripStatusLabel控件名称</param>
        /// <param name="text">需要显示的文本</param>
        public static void ShowText(ToolStripStatusLabel stslabel, string text)
        {
            if ((stslabel.Owner).InvokeRequired)
            {
                (stslabel.Owner).Invoke(new Action<ToolStripStatusLabel, string>((sts, str) => ShowText(sts, str)), stslabel, text);
                return;
            }
            stslabel.Text = text;
        }
        /// <summary>
        /// 在ToolStripStatusLabel上修改Text,背景色和前景色，如果前景色或者背景色值为Color.Empty时，则不发生改变
        /// </summary>
        /// <param name="stslabel">需要被改变的ToolStripStatusLabel控件名称</param>
        /// <param name="text">显示在控件上的字符串</param>
        /// <param name="backcolor">控件的背景色</param>
        /// <param name="foreColor">控件的前景色</param>
        public static void ShowText(ToolStripStatusLabel stslabel, string text, Color backcolor, Color foreColor)
        {
            if ((stslabel.Owner).InvokeRequired)
            {
                (stslabel.Owner).Invoke(new Action<ToolStripStatusLabel, string, Color, Color>((sts, str, bclr, fclr) => ShowText(sts, str, bclr, fclr)), stslabel, text, backcolor, foreColor);
                return;
            }
            stslabel.Text = text;
            if (backcolor != Color.Empty)
            {
                stslabel.BackColor = backcolor;
            }
            if (foreColor != Color.Empty)
            {
                stslabel.ForeColor = foreColor;
            }
        }

        /// <summary>
        /// 在指定Control类控件上显示Text属性值
        /// </summary>
        /// <param name="control">控件的名称</param>
        /// <param name="text">需要显示的文本</param>
        public static void ShowText(Control control, string text)
        {
            if (control.InvokeRequired)
            {
                control.Invoke(new Action<Control, string>((ctr, str) => ShowText(ctr, str)), control, text);
                return;
            }
            control.Text = text;
        }
        /// <summary>
        /// 在指定Control类控件上显示Text，背景色和前景色，如果前景色或者背景色值为Color.Empty时，则不发生改变
        /// </summary>
        /// <param name="control">需要被改变的Control类控件名称</param>
        /// <param name="text">显示在控件上的字符串</param>
        /// <param name="backColor">控件背景色</param>
        /// <param name="foreColor">控件前景色</param>
        public static void ButtonChange(Button btn, Color backColor, Color foreColor)
        {
            if (btn.InvokeRequired)
            {
                btn.Invoke(new Action<Button, Color, Color>(( butn, bclr, fclr) => ButtonChange( butn, bclr, fclr)), btn, backColor, foreColor);
                return;
            }
            //control.Text = text;
            if (backColor != Color.Empty)
            {
                btn.BackColor = backColor;
            }
            if (foreColor != Color.Empty)
            {
                btn.ForeColor = foreColor;

            }

        }
        /// <summary>
        /// 在指定Control类控件上显示Text，背景色和前景色，如果前景色或者背景色值为Color.Empty时，则不发生改变
        /// </summary>
        /// <param name="control">需要被改变的Control类控件名称</param>
        /// <param name="text">显示在控件上的字符串</param>
        /// <param name="backColor">控件背景色</param>
        /// <param name="foreColor">控件前景色</param>
        public static void ShowText(Control control, string text, Color backColor, Color foreColor)
        {
            if (control.InvokeRequired)
            {
                control.Invoke(new Action<Control, string, Color, Color>((sts, str, bclr, fclr) => ShowText(sts,  str, bclr, fclr)), control,  text, backColor, foreColor);
                return;
            }
            control.Text = text;
            if (backColor != Color.Empty)
            {
                control.BackColor = backColor;
            }
            if (foreColor != Color.Empty)
            {
                control.ForeColor = foreColor;

            }
        }
        public static void ShowText(Control control, string text, Color backColor, Color foreColor, bool font, float sz)
        {
            if (control.InvokeRequired)
            {
                control.Invoke(new Action<Control, string, Color, Color, bool, float>((sts, str, bclr, fclr, ftt, szz) => ShowText(sts, str, bclr, fclr, ftt, szz)), control, text, backColor, foreColor, font, sz);
                return;
            }
            control.Text = text;
            if (backColor != Color.Empty)
            {
                control.BackColor = backColor;
            }
            if (foreColor != Color.Empty)
            {
                control.ForeColor = foreColor;
            }
            if (font)
            {
                control.Font = new System.Drawing.Font("微软雅黑", sz, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            }

        }

        /// <summary>
        /// 在指定Control类控件上显示Value属性值
        /// </summary>
        /// <param name="dgv">需要被改变的Datagridview控件名称</param>
        /// <param name="row">被改变的单元格行索引</param>
        /// <param name="col">被改变的单元格列索引</param>
        /// <param name="text">需要显示在指定单元格的字符串</param>
        public static void ShowText(DataGridView dgv, int row, int col, string text)
        {
            if (dgv.Created && dgv.InvokeRequired)
            {
                dgv.Invoke(new Action<DataGridView, int, int, string>((dgview, rowindex, colindex, str) => ShowText(dgview, rowindex, colindex, str)), dgv, row, col, text);
                return;
            }
            if (dgv.RowCount > row && dgv.ColumnCount > col)
            {
                dgv.Rows[row].Cells[col].Value = text;
            }
        }

        /// <summary>
        /// 在指定的Datagridview控件中修改指定单元格的Text属性,并指定背景色和前景色，如果前景色或者背景色值为Color.Empty时，则不发生改变
        /// </summary>
        /// <param name="dgv">需要被改变的Datagridview控件名称</param>
        /// <param name="row">被改变的单元格行索引</param>
        /// <param name="col">被改变的单元格列索引</param>
        /// <param name="text">需要显示在指定单元格的字符串</param>
        /// <param name="backColor">单元格背景色</param>
        /// <param name="foreColor">单元格前景色</param>
        public static void ShowText(DataGridView dgv, int row, int col, string text, Color backColor, Color foreColor)
        {
            if (dgv.Created && dgv.InvokeRequired)
            {
                dgv.Invoke(new Action<DataGridView, int, int, string, Color, Color>((dgview, rowindex, colindex, str, bclr, fclr) => ShowText(dgview, rowindex, colindex, str, bclr, fclr)), dgv, row, col, text, backColor, foreColor);
                return;
            }
            if (dgv.RowCount > row && dgv.ColumnCount > col)
            {
                dgv.Rows[row].Cells[col].Value = text;
            }
            if (backColor != Color.Empty)
            {
                dgv.Rows[row].Cells[col].Style.BackColor = backColor;
            }
            if (foreColor != Color.Empty)
            {
                dgv.Rows[row].Cells[col].Style.ForeColor = foreColor;
            }

        }

        /// <summary>
        /// 在指定的Datagridview控件中新增一行数据
        /// </summary>
        /// <param name="dgv">需要增加一行的Datagridview控件名称</param>
        /// <param name="objs">需要填充到最新一行的数据数组</param>
        public static void AddRowOnDgv(DataGridView dgv, object[] objs)
        {
            if (dgv.InvokeRequired)
            {
                dgv.Invoke(new Action<DataGridView, object[]>((dgview, obj) => AddRowOnDgv(dgview, obj)), dgv, objs);
                return;
            }
            dgv.Rows.Add();
            for (int i = 0; i < objs.Length; i++)
            {
                if (i < dgv.ColumnCount)
                {
                    dgv.Rows[dgv.RowCount - 1].Cells[i].Value = objs[i].ToString();
                }
            }

            dgv.FirstDisplayedScrollingRowIndex = dgv.RowCount - 1;
        }
        /// <summary>
        /// 在指定的Datagridview控件中的指定位置插入一行
        /// </summary>
        /// <param name="dgv">需要增加一行的Datagridview控件名称</param>
        /// <param name="index">序号</param>
        public static void InsertRow(DataGridView dgv, int index)
        {
            if (dgv.InvokeRequired)
            {
                dgv.Invoke(new Action<DataGridView, int>((dgview, i) => InsertRow(dgview, i)), dgv, index);
                return;
            }
            dgv.Rows.Insert(index, new DataGridViewRow());
        }

        /// <summary>
        /// 在指定的Datagridview控件中新增一行数据,并指定背景色和前景色，如果前景色或者背景色值为Color.Empty时，则不发生改变
        /// </summary>
        /// <param name="dgv">需要增加一行的Datagridview控件名称</param>
        /// <param name="objs">需要填充到最新一行的数据数组</param>
        /// <param name="backcolor">添加行的背景色</param>
        /// <param name="forecolor">添加行的前景色</param>
        public static void AddRowOnDgv(DataGridView dgv, object[] objs, Color backcolor, Color forecolor)
        {
            if (dgv.InvokeRequired)
            {
                dgv.Invoke(new Action<DataGridView, object[], Color, Color>((dgview, obj, bclr, fclr) => AddRowOnDgv(dgview, obj, bclr, fclr)), dgv, objs, backcolor, forecolor);
                return;
            }
            dgv.Rows.Add();
            if (backcolor != Color.Empty)
            {
                dgv.Rows[dgv.RowCount - 1].DefaultCellStyle.BackColor = backcolor;
            }
            if (forecolor != Color.Empty)
            {
                dgv.Rows[dgv.RowCount - 1].DefaultCellStyle.ForeColor = forecolor;
            }
            for (int i = 0; i < objs.Length; i++)
            {
                if (i < dgv.ColumnCount)
                {
                    dgv.Rows[dgv.RowCount - 1].Cells[i].Value = objs[i].ToString();
                }
            }

            dgv.FirstDisplayedScrollingRowIndex = dgv.RowCount - 1;
        }

        /// <summary>
        /// 改变TabControl控件的选中页面
        /// </summary>
        /// <param name="tp">TabControl的控件名称</param>
        /// <param name="index">需要的页面索引值</param>
        public static void PageSelect(TabControl tp, int index)
        {
            if (tp.Created && tp.InvokeRequired)
            {
                tp.Invoke(new Action<TabControl, int>((tabp, i) => PageSelect(tabp, i)), tp, index);
                return;
            }
            tp.SelectedIndex = index;
        }

        /// <summary>
        /// 在RichTextBox控件中追加数据
        /// </summary>
        /// <param name="rtbox">RichTextBox控件名称</param>
        /// <param name="text">需要追加的字符串内容</param>
        /// <param name="addTime">是否在追加内容上增加当前时间</param>
        public static void AddTextToRichTextBox(RichTextBox rtbox, string text, bool addTime)
        {
            if (rtbox.InvokeRequired)
            {
                rtbox.Invoke(new Action<RichTextBox, string, bool>((rtb, str, addtime) => AddTextToRichTextBox(rtb, str, addtime)), rtbox, text, addTime);
                return;
            }

            if (addTime)
            {
                text = string.Format("【{0}】--{1}", DateTime.Now.ToString(), text);
            }

            rtbox.AppendText(text + "\r\n");

            rtbox.ScrollToCaret();
        }

        public static void AddTextToListBox(ListBox ltbox, string text, bool addTime)
        {
            if (ltbox.InvokeRequired)
            {
                ltbox.Invoke(new Action<ListBox, string, bool>((ltb, str, addtime) => AddTextToListBox(ltb, str, addtime)), ltbox, text, addTime);
                return;
            }

            if (addTime)
            {
                text = string.Format("【{0}】--{1}", DateTime.Now.ToString(), text);
            }

            ltbox.Items.Add(text + "\r\n");

        }
        public static void AddTextToListBoxRe(ListBox ltbox, string text, bool addTime)
        {
            if (ltbox.InvokeRequired)
            {
                ltbox.Invoke(new Action<ListBox, string, bool>((ltb, str, addtime) => AddTextToListBoxRe(ltb, str, addtime)), ltbox, text, addTime);
                return;
            }

            if (addTime)
            {
                text = string.Format("【{0}】--{1}", DateTime.Now.ToString(), text);
            }

            ltbox.Items.Insert(0, text);
        }
        /// <summary>
        /// 在RichTextBox控件中追加数据，并指定当前内容显示的前景色，如果前景色值为Color.Empty时，则不发生改变
        /// </summary>
        /// <param name="rtbox">RichTextBox控件名称</param>
        /// <param name="text">需要追加的字符串内容</param>
        /// <param name="color">追加内容的前景色</param>
        /// <param name="addTime">是否在追加内容上增加当前时间</param>
        public static void AddTextToRichTextBox(RichTextBox rtbox, string text, Color color, bool addTime)
        {

            if (rtbox.InvokeRequired)
            {
                rtbox.BeginInvoke(new Action<RichTextBox, string, Color, bool>((rtb, str, forecolor, addtime) => AddTextToRichTextBox(rtb, str, forecolor, addtime)), rtbox, text, color, addTime);
                return;
            }

            if (addTime)
            {
                text = string.Format("【{0}】--{1}", DateTime.Now.ToString(), text);
            }
            if (color != Color.Empty)
            {
                try
                {
                    rtbox.SelectionColor = color;
                }
                catch
                {
                }

            }

            try
            {
                rtbox.AppendText(text + "\r\n");

                rtbox.ScrollToCaret();
            }
            catch (Exception)
            {

            }

        }
        /// <summary>
        /// 在RichTextBox控件中追加数据，并指定当前内容显示的字体，如果字体为Color.Empty时，则字体不发生改变
        /// </summary>
        /// <param name="rtbox">RichTextBox控件名称</param>
        /// <param name="text">需要追加的字符串内容</param>
        /// <param name="font">追加内容的字体</param>
        /// <param name="addTime">是否在追加内容上增加当前时间</param>
        public static void AddTextToRichTextBox(RichTextBox rtbox, string text, Font font, bool addTime)
        {

            if (rtbox.InvokeRequired)
            {
                rtbox.Invoke(new Action<RichTextBox, string, Font, bool>((rtb, str, fnt, addtime) => AddTextToRichTextBox(rtb, str, fnt, addtime)), rtbox, text, font, addTime);
                return;
            }

            if (addTime)
            {
                text = string.Format("【{0}】--{1}", DateTime.Now.ToString(), text);
            }
            if (font != null)
            {
                rtbox.SelectionFont = font;
            }

            rtbox.AppendText(text + "\r\n");

            rtbox.ScrollToCaret();

        }
        /// <summary>
        /// 在RichTextBox控件中追加数据，并指定当前内容显示的前景色，如果前景色值为Color.Empty时，则不发生改变,如果字体为null时，则字体不发生改变
        /// </summary>
        /// <param name="rtbox">RichTextBox控件名称</param>
        /// <param name="text">需要追加的字符串内容</param>
        /// <param name="color">追加内容的前景色</param>
        /// <param name="font">追加内容的字体</param>
        /// <param name="addTime">是否在追加内容上增加当前时间</param>
        public static void AddTextToRichTextBox(RichTextBox rtbox, string text, Color color, Font font, bool addTime)
        {
            if (rtbox.InvokeRequired)
            {
                rtbox.Invoke(new Action<RichTextBox, string, Color, Font, bool>((rtb, str, forecolor, fnt, addtime) => AddTextToRichTextBox(rtb, str, forecolor, fnt, addtime)), rtbox, text, color, font, addTime);
                return;
            }

            if (addTime)
            {
                text = string.Format("【{0}】--{1}", DateTime.Now.ToString(), text);
            }
            if (color != Color.Empty)
            {
                rtbox.SelectionColor = color;
            }

            if (font != null)
            {
                rtbox.SelectionFont = font;
            }

            rtbox.AppendText(text + "\r\n");

            rtbox.ScrollToCaret();
        }
        /// <summary>
        /// 清空指定RichTextBox控件的所有内容
        /// </summary>
        /// <param name="rtbox">RichTextBox的名称</param>
        public static void ClearRichTextBox(RichTextBox rtbox)
        {
            if (rtbox.InvokeRequired)
            {
                rtbox.Invoke(new Action<RichTextBox>((rtb) => ClearRichTextBox(rtb)), rtbox);
                return;
            }

            rtbox.ResetText();
        }
        /// <summary>
        /// 获取指定RichTextBox控件的所有内容
        /// </summary>
        /// <param name="rtbox">RichTextBox的名称</param>
        public static void GetAllText(RichTextBox rtbox)
        {
            if (rtbox.InvokeRequired)
            {
                rtbox.Invoke(new Action<RichTextBox>((rtb) => GetAllText(rtb)), rtbox);
                return;
            }
            RtbStrings = new string[] { };
            RtbStrings = rtbox.Lines;
        }

        /// <summary>
        /// 是否启用指定的Button控件
        /// </summary>
        /// <param name="btn">指定的Button控件的名称</param>
        /// <param name="enable">是否启用</param>
        public static void ButtonEnable(Button btn, bool enable)
        {
            if (btn.InvokeRequired)
            {
                btn.Invoke(new Action<Button, bool>((b, e) => ButtonEnable(b, e)), btn, enable);
                return;
            }
            btn.Enabled = enable;
        }


        /// <summary>
        /// 向ListBox中添加数据
        /// </summary>
        /// <param name="listBoxName">控件名称</param>
        /// <param name="Text">添加内容</param>
        public static void AddList(ListBox listBoxName, string Text)
        {
            if (listBoxName.InvokeRequired)
            {
                listBoxName.Invoke(new Action(() =>
                {
                    if (listBoxName.Items.Count > 10) listBoxName.Items.RemoveAt(listBoxName.Items.Count - 1);
                    listBoxName.Items.Insert(0, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + Text);
                }));
            }
            else
            {
                if (listBoxName.Items.Count > 10) listBoxName.Items.RemoveAt(listBoxName.Items.Count - 1);
                listBoxName.Items.Insert(0, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + Text);
            }

        }

        /// <summary>
        /// 向RichTextBox中添加内容
        /// </summary>
        /// <param name="RichName">控件名称</param>
        /// <param name="Text">文本内容</param>
        public static void AddRichTextBox(RichTextBox RichName, string Text)
        {
            if (RichName.InvokeRequired)
            {
                RichName.Invoke(new Action(() =>
                {
                    try
                    {
                        RichName.AppendText(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + Text + "\r\n");
                        RichName.ScrollToCaret();
                    }
                    catch (Exception)
                    {

                    }
                }));
            }
            else
            {
                try
                {
                    RichName.AppendText(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + Text + "\r\n");

                    RichName.ScrollToCaret();
                }
                catch (Exception)
                {

                }
            }
        }

        /// <summary>
        /// 是否启用指定的控件
        /// </summary>
        /// <param name="btn">指定的控件的名称</param>
        /// <param name="enable">是否启用</param>
        public static void ControlEnable(Control btn, bool enable)
        {
            if (btn.InvokeRequired)
            {
                btn.Invoke(new Action<Button, bool>((b, e) => ControlEnable(b, e)), btn, enable);
                return;
            }
            btn.Enabled = enable;
        }
        private delegate void ShowPictureOnDgvEventHandler(Graphics _g, PictureBox pb, Rectangle a, Rectangle b, Bitmap m_bitmap);
        public static void ShowPicture(Graphics _g, PictureBox pb, Rectangle a, Rectangle b, Bitmap m_bitmap)
        {
            if (pb.InvokeRequired)
            {
                pb.BeginInvoke(new ShowPictureOnDgvEventHandler(ShowPicture), new object[] { _g, pb, a, b , m_bitmap });
                return;
            }
            if (_g == null)
            {
                _g = pb.CreateGraphics();
                a.Width = pb.Width;
                a.Height = pb.Height;
            }
            _g.DrawImage(m_bitmap, a, b, GraphicsUnit.Pixel);
        }
    }


    #endregion



}

